<template>
  <el-dialog
    title="选择用户"
    :visible.sync="dialogData.isShow"
    :before-close="labelHandleClose"
    width="800px"
  >
    <!-- 搜索 -->
    <div class="search-box">
      <el-form
        ref="ruleForm"
        :inline="true"
        :model="formInline"
        :rules="rules"
        class="demo-form-inline"
      >
        <el-form-item label="登录名称" prop="loginName">
          <el-input
            v-model="formInline.loginName"
            placeholder="请输入登录名称搜索"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input
            v-model="formInline.phonenumber"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <el-form-item class="s-btn">
          <el-button
            type="success"
            icon="el-icon-search"
            @click="toSearch('ruleForm')"
          >
            查询
          </el-button>
          <el-button type="success" plain @click="reset">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      stripe
      style="width: 100%"
      height="450"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      ></el-table-column>
      <el-table-column
        label="登录名称"
        prop="loginName"
        show-overflow-tooltip
        min-width="100"
      ></el-table-column>
      <el-table-column
        label="用户名称"
        prop="userName"
        min-width="100"
      ></el-table-column>
      <el-table-column
        label="邮箱"
        prop="email"
        min-width="120"
      ></el-table-column>
      <el-table-column
        label="手机"
        prop="phonenumber"
        min-width="120"
      ></el-table-column>
      <el-table-column label="用户状态" prop="status" min-width="100">
        <template>
          <span class="tag-span primary">正常</span>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="createTime"
        min-width="150"
      ></el-table-column>
    </el-table>
    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
    ></pageCom>
    <!-- 提交按钮 -->
    <div slot="footer" class="dialog-footer">
      <el-button type="success" plain @click="cancelLabel">
        取 消
      </el-button>
      <el-button type="success" @click="handelAdd">
        确 定
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import { assigningUsers, subUsers } from '@/api/system/roles' // 接口
export default {
  name: 'AddSetUser',
  props: ['dialogData'],
  data() {
    return {
      loading: false,
      total: 0,
      tableData: [], // table内容
      multipleSelection: [], // 选中的行
      rules: {
        loginName: {},
        phonenumber: {}
      },
      searchParams: {
        pageNum: 1, // 当前页数
        pageSize: 10 // 页面显示条数
      },
      formInline: {
        loginName: '',
        phonenumber: ''
      }
    }
  },
  mounted() {
    this.roleId = this.dialogData.params
    this.searchList()
  },
  methods: {
    /* 获取列表 */
    async searchList() {
      this.loading = true
      let obj = Object.assign({}, this.searchParams, this.formInline)
      await assigningUsers(
        this.roleId,
        obj,
        (response) => {
          this.loading = false
          this.tableData = response.rows
          this.total = response.total
        },
        (response) => {
          this.loading = false
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    // 提交时间
    handelAdd() {
      if (this.multipleSelection.length === 0) {
        this.$alert('请至少选择一条记录', '提示', {
          confirmButtonText: '确定'
        })
      } else {
        let userIds = []
        this.multipleSelection.forEach((item) => {
          userIds.push(item.userId)
        })
        this.handleItem(userIds)
      }
    },
    async handleItem(userIds) {
      this.loading = true
      let obj = { userIds }
      obj.rolesId = this.roleId
      await subUsers(
        this.roleId,
        obj,
        (response) => {
          this.loading = false
          this.$emit('refresh')
          this.cancelLabel()
        },
        (response) => {
          this.loading = false
          this.$message({
            message: '分配用户失败',
            type: 'warning'
          })
        }
      )
    },
    labelHandleClose(done) {
      this.cancelLabel()
      done()
    },
    // 打开dialog弹出框
    cancelLabel() {
      // 关闭前清空
      this.loading = false
      this.dialogData.isShow = false
    },
    /* 搜索 */
    toSearch() {
      this.searchParams.pageNum = 1
      this.searchList()
    },

    /* 重置 */
    reset() {
      this.$refs['ruleForm'].resetFields()
    },
    /* 换页 */
    changePage(page) {
      this.searchParams.pageNum = page
      this.searchList()
    },
    // checkbox选中事件
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</script>
<style lang="less">
.el-dialog {
  border-radius: 4px;
  .el-dialog__header {
    border-bottom: 1px solid rgba(225, 228, 233, 1);
  }
  .el-dialog__body {
    padding: 30px 20px 0;
  }
  .page {
    margin-top: 15px;
  }
  .el-dialog__footer {
    padding: 0 20px 20px;
  }
}
</style>
